
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.tompai.common.bean;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.rits.cloning.Cloner;

/**
* @desc: tompai-common
* @name: CommonBeanUtils.java
* @author: tompai
* @email：liinux@qq.com
* @createTime: 2021年2月7日 下午11:57:37
* @history:
* @version: v1.0
*/

public class CommonBeanUtils {
	
	private static final Cloner cloner = new Cloner();

	/**
	 * 复制对象（深度拷贝）
	 * @param object
	 * @param <T>
	 * @return
	 */
	public static <T> T clone(final T object) {
		if (object == null) {
			return null;
		}
		return cloner.deepClone(object);
	}

	/**
	 * 复制集合（深度拷贝）
	 * @param object
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> cloneList(final List<T> object) {
		if (object == null) {
			return null;
		}
		return cloner.deepClone(object);
	}

	/**
	 * 复制对象到指定类（深度拷贝）
	 * @param object
	 * @param destclas 指定类
	 * @param <T>
	 * @return
	 */
	public static <T> T clone(final Object object, Class<T> destclas) {
		if (object == null) {
			return null;
		}
		String json = JSON.toJSONString(object);
		return JSON.parseObject(json, destclas);
	}

	/**
	 * 复制集合到指定类（深度拷贝）
	 * @param object
	 * @param destclas 指定类
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> cloneList(List<?> object, Class<T> destclas) {
		if (object == null) {
			return new ArrayList<T>();
		}
		String json = JSON.toJSONString(object);
		return JSON.parseArray(json, destclas);
	}
}
